Skip to content

C#: Data flow for positional patterns. - #22689

Merged
michaelnebel merged 8 commits into
github:mainfrom
michaelnebel:csharp/positionalpatternflow
Oct 5, 2026
Merged

michaelnebel merged 8 commits into
github:mainfrom
michaelnebel:csharp/positionalpatternflow

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

In this PR we add data flow support via positional patterns. That is,

var x = (1, (2, 3), 4);
switch (x)
{
    case (var a1, (var b1, var c1), _):
        Sink(a1);        // has value flow 1
        Sink(c1);        // has value flow 3
        Sink(b1);       // has value flow 2.
        break;
}

Note, that positional patterns are similar to tuple patterns (var (x, y)), but they haven't been handled in the data flow library before now.

DCA looks good.

@michaelnebel michaelnebel changed the title Csharp/positionalpatternflow C#: Data flow for positional patterns. Sep 29, 2026
@michaelnebel
michaelnebel force-pushed the csharp/positionalpatternflow branch from 84d670d to f059beb Compare September 30, 2026 12:22
@michaelnebel
michaelnebel force-pushed the csharp/positionalpatternflow branch from f059beb to f3eaab2 Compare October 2, 2026 08:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Nested positional patterns that capture the whole inner tuple still lose flow to the capture variable.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds tuple-backed positional-pattern support to C# data-flow analysis, including nested patterns.

Changes:

  • Models flow through positional patterns in is expressions and switch cases.
  • Adds nested-pattern tests and updates generated expectations.
  • Documents the analysis improvement.
File Description
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​Tuples.expected Updates expected flow results.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​Tuples.cs Adds nested positional-pattern tests.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​PrintAst.expected Updates expected test syntax trees.
csharp/​ql/​test/​library-tests/​dataflow/​tuples/​DataFlowStep.expected Updates expected flow steps.
csharp/​ql/​lib/​semmle/​code/​csharp/​dataflow/​internal/​DataFlowPrivate.qll Adds positional-pattern flow modeling.
csharp/​ql/​lib/​change-notes/​2026-10-02-positional-pattern-flow.md Documents the new support.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll Outdated
@michaelnebel
michaelnebel requested a review from hvitved October 5, 2026 09:00
@michaelnebel
michaelnebel marked this pull request as ready for review October 5, 2026 09:00
@michaelnebel
michaelnebel requested a review from a team as a code owner October 5, 2026 09:00

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but did you consider modeling this as calls to Deconstruct? Then we would have to define MaD models for tuple deconstruct methods.

@michaelnebel

Copy link
Copy Markdown
Contributor Author

LGTM, but did you consider modeling this as calls to Deconstruct? Then we would have to define MaD models for tuple deconstruct methods.

No, I must admit, that I didn't consider it. Deconstruction also applies for other cases (like var (x,y) = r) and tuple pattern matching in general, and I wanted to get a working implementation for pattern matching without also changing the existing implementation.
If time allows it, I will look into generalizing using Deconstruction. Is that acceptable?

@michaelnebel
michaelnebel merged commit 1d349af into github:main Oct 5, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants